package com.hui.app;

import com.hui.entity.Phone;
import com.hui.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;

import java.sql.SQLException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/06/14/9:54
 * @Description:
 */
public class PhoneApp {
    private QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());

    // 查询价格高于2000元，生产日期是2019年之前的所有手机
    @Test
    public void select() throws SQLException {
        String sql = "select * from phone where price > ? and prodate < ?";
        List<Phone> phoneList = qr.query(sql, new BeanListHandler<>(Phone.class), 2000, "2019-01-01");
        for (Phone phone : phoneList) {
            System.out.println(phone);
        }
    }

    // 查询所有颜色是白色的手机信息
    @Test
    public void select2() throws SQLException {
        String sql = "select * from phone where color = ? ";
        List<Phone> phoneList = qr.query(sql, new BeanListHandler<>(Phone.class), "白色");
        for (Phone phone : phoneList) {
            System.out.println(phone);
        }
    }
}
